﻿using Azure;
using Customer.Domain;
using Customer.Domain.Dto;
using Customer.Read.API.Applcation.Command.ClientCommand;
using Customer.Read.API.Applcation.Command.VendorCommand;
using Customer.Read.API.Applcation.CommandHandler.ClientCommanfHandler;
using Customer.Read.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Warehouse.Domain.DTO;

namespace Customer.Read.API.Controllers
{
    /// <summary>
    /// 客户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    
    public class ClientReadController : ControllerBase
    {
        private readonly IMediator _mediator;
        public ClientReadController(IMediator _mediator)
        {
            this._mediator = _mediator;
        }
        /// <summary>
        /// 反填客户
        /// </summary>
        /// <param name="command">要反填的数据id</param>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet]
        public async Task<ApiResult<List<FanClientDTO>>> GetClientById([FromQuery] ClientFanTCommand command)
        {
            ApiResult<List<FanClientDTO>> result = new ApiResult<List<FanClientDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 客户下拉框
        /// </summary>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet]
        public async Task<ApiResult<List<Client>>> GetDropClient()
        {
            ApiResult<List<Client>> result = new ApiResult<List<Client>>();
            try
            {
                result.Data = await _mediator.Send(new ClientDropCommand());
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 客户列表
        /// </summary>
        /// <param name="command">查询条件</param>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet]
        public async Task<ApiResult<Domain.Dto.Page<ClientDto>>> GetClient([FromQuery]SelectClientsCommand command)
        {
            ApiResult<Domain.Dto.Page<ClientDto>> result = new ApiResult<Domain.Dto.Page<ClientDto>>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 客户信息表导出
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FileContentResult> GetAllVendorExel([FromQuery] SelectClientsCommand command)
        {
            var obj = await _mediator.Send(command);
            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("客户信息表");
            IRow row;
            row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("客户编号");
            row.CreateCell(1).SetCellValue("客户名称");
            row.CreateCell(2).SetCellValue("传真");
            row.CreateCell(3).SetCellValue("电话");
            row.CreateCell(4).SetCellValue("邮箱");
            row.CreateCell(5).SetCellValue("备注");
            int index = 1;
            foreach (var item in obj.list)
            {
                row = sheet.CreateRow(index);
                row.CreateCell(0).SetCellValue(item.ClientCode);
                row.CreateCell(1).SetCellValue(item.ClientName);
                row.CreateCell(2).SetCellValue(item.Cfax);
                row.CreateCell(3).SetCellValue(item.Landline);
                row.CreateCell(4).SetCellValue(item.Mailbox);
                row.CreateCell(5).SetCellValue(item.Cremark);
                index++;
            }
            byte[] buffe;
            using (MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                buffe = ms.ToArray();
            }
            string dslist = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(buffe, dslist, "客户信息表.xlsx");
        }

        [HttpGet]
        public async Task<ApiResult<ClientCusContactDTO>> GetClientCusContactById([FromQuery]ClientCusContactFanCommand command)
        {
            ApiResult<ClientCusContactDTO> result = new ApiResult<ClientCusContactDTO>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
    }
}
